package org.jks.util;

import java.io.UnsupportedEncodingException;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HtmlUtil {
	public static String getBrief(String content) {
		String str = "";
		str = extractText(content);
		
		if (str.length() > 100) {
			str = str.substring(0, 100);
			str += "...";
		}
		return str;
	}

	public static String extractText(String inputHtml) {
		StringBuffer text = new StringBuffer();
		Parser parser;
		try {
			parser = Parser.createParser(new String(inputHtml.getBytes(),
					"UTF-8"), "UTF-8");
			NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
				private static final long serialVersionUID = 1L;

				public boolean accept(Node node) {
					return true;
				}
			});

			for (int i = 0; i < nodes.size(); i++) {
				Node nodet = nodes.elementAt(i);
				text.append(new String(nodet.toPlainTextString().getBytes("UTF-8"))
						+ "\r\n");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserException e) {
			e.printStackTrace();
		}
		
		return text.toString();
	}
}
